package cn.piflow.api

import cn.piflow.api.util.PropertyUtil
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{HttpClientBuilder, HttpClients}
import org.apache.http.util.EntityUtils

object HTTPClientStartFlow {

  def main(args: Array[String]): Unit = {
    //val json:String = """{"flow":{"name":"Flow","uuid":"1234","stops":[{"uuid":"1111","name":"XmlParser","bundle":"cn.piflow.bundle.xml.XmlParser","properties":{"xmlpath":"hdfs://10.0.86.89:9000/xjzhu/dblp.mini.xml","rowTag":"phdthesis"}},{"uuid":"2222","name":"SelectField","bundle":"cn.piflow.bundle.common.SelectField","properties":{"schema":"title,author,pages"}},{"uuid":"3333","name":"PutHiveStreaming","bundle":"cn.piflow.bundle.hive.PutHiveStreaming","properties":{"database":"sparktest","table":"dblp_phdthesis"}},{"uuid":"4444","name":"CsvParser","bundle":"cn.piflow.bundle.csv.CsvParser","properties":{"csvPath":"hdfs://10.0.86.89:9000/xjzhu/phdthesis.csv","header":"false","delimiter":",","schema":"title,author,pages"}},{"uuid":"555","name":"Merge","bundle":"cn.piflow.bundle.common.Merge","properties":{}},{"uuid":"666","name":"Fork","bundle":"cn.piflow.bundle.common.Fork","properties":{"outports":["out1","out2","out3"]}},{"uuid":"777","name":"JsonSave","bundle":"cn.piflow.bundle.json.JsonSave","properties":{"jsonSavePath":"hdfs://10.0.86.89:9000/xjzhu/phdthesis.json"}},{"uuid":"888","name":"CsvSave","bundle":"cn.piflow.bundle.csv.CsvSave","properties":{"csvSavePath":"hdfs://10.0.86.89:9000/xjzhu/phdthesis_result.csv","header":"true","delimiter":","}}],"paths":[{"from":"XmlParser","outport":"","inport":"","to":"SelectField"},{"from":"SelectField","outport":"","inport":"data1","to":"Merge"},{"from":"CsvParser","outport":"","inport":"data2","to":"Merge"},{"from":"Merge","outport":"","inport":"","to":"Fork"},{"from":"Fork","outport":"out1","inport":"","to":"PutHiveStreaming"},{"from":"Fork","outport":"out2","inport":"","to":"JsonSave"},{"from":"Fork","outport":"out3","inport":"","to":"CsvSave"}]}}"""
    val json ="""
        |{
        |  "flow":{
        |    "name":"xml,csv-merge-fork-hive,json,csv",
        |    "uuid":"1234",
        |    "checkpoint":"Merge",
        |    "runMode":"DEBUG",
        |    "stops":[
        |      {
        |        "uuid":"1111",
        |        "name":"XmlParser",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |            "xmlpath":"hdfs://10.0.86.89:9000/xjzhu/dblp.mini.xml",
        |            "rowTag":"phdthesis"
        |        }
        |
        |      },
        |      {
        |        "uuid":"2222",
        |        "name":"SelectField",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |            "schema":"title,author,pages"
        |        }
        |
        |      },
        |      {
        |        "uuid":"3333",
        |        "name":"PutHiveStreaming",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |            "database":"sparktest",
        |            "table":"dblp_phdthesis"
        |        }
        |      },
        |      {
        |        "uuid":"4444",
        |        "name":"CsvParser",
        |        "bundle":"cn.piflow.bundle.csv.CsvParser",
        |        "properties":{
        |            "csvPath":"hdfs://10.0.86.89:9000/xjzhu/phdthesis.csv",
        |            "header":"false",
        |            "delimiter":",",
        |            "schema":"title,author,pages"
        |        }
        |      },
        |      {
        |        "uuid":"555",
        |        "name":"Merge",
        |        "bundle":"cn.piflow.bundle.common.Merge",
        |        "properties":{
        |          "inports":"data1,data2"
        |        }
        |      },
        |      {
        |        "uuid":"666",
        |        "name":"Fork",
        |        "bundle":"cn.piflow.bundle.common.Fork",
        |        "properties":{
        |          "outports":"out1,out2,out3"
        |        }
        |      },
        |      {
        |        "uuid":"777",
        |        "name":"JsonSave",
        |        "bundle":"cn.piflow.bundle.json.JsonSave",
        |        "properties":{
        |          "jsonSavePath":"hdfs://10.0.86.89:9000/xjzhu/phdthesis.json"
        |        }
        |      },
        |      {
        |        "uuid":"888",
        |        "name":"CsvSave",
        |        "bundle":"cn.piflow.bundle.csv.CsvSave",
        |        "properties":{
        |          "csvSavePath":"hdfs://10.0.86.89:9000/xjzhu/phdthesis_result.csv",
        |          "header":"true",
        |          "delimiter":","
        |        }
        |      }
        |    ],
        |    "paths":[
        |      {
        |        "from":"XmlParser",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField"
        |      },
        |      {
        |        "from":"SelectField",
        |        "outport":"",
        |        "inport":"data1",
        |        "to":"Merge"
        |      },
        |      {
        |        "from":"CsvParser",
        |        "outport":"",
        |        "inport":"data2",
        |        "to":"Merge"
        |      },
        |      {
        |        "from":"Merge",
        |        "outport":"",
        |        "inport":"",
        |        "to":"Fork"
        |      },
        |      {
        |        "from":"Fork",
        |        "outport":"out1",
        |        "inport":"",
        |        "to":"PutHiveStreaming"
        |      },
        |      {
        |        "from":"Fork",
        |        "outport":"out2",
        |        "inport":"",
        |        "to":"JsonSave"
        |      },
        |      {
        |        "from":"Fork",
        |        "outport":"out3",
        |        "inport":"",
        |        "to":"CsvSave"
        |      }
        |    ]
        |  }
        |}
      """.stripMargin
    /*val json=
      """
        |{
        |  "flow":{
        |    "name":"test",
        |    "uuid":"1234",
        |    "checkpoint":"Merge",
        |    "stops":[
        |      {
        |        "uuid":"1111",
        |        "name":"UnzipFilesOnHDFS",
        |        "bundle":"cn.piflow.bundle.hdfs.UnzipFilesOnHDFS",
        |        "properties":{
        |          "isCustomize":"true",
        |          "hdfsUrl":"hdfs://10.0.88.70:9000",
        |          "filePath":"/yqd/dblp/dblp.xml.gz",
        |          "savePath": ""
        |        }
        |      },
        |      {
        |        "uuid":"2222",
        |        "name":"Fork",
        |        "bundle":"cn.piflow.bundle.common.Fork",
        |        "properties":{
        |          "outports":"out1,out2,out3,out4,out5,out6,out7,out8"
        |        }
        |      },
        |      {
        |        "uuid":"3333",
        |        "name":"XmlParser1",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |          "xmlpath":"hdfs://10.0.88.70:9000/yqd/dblp/dblp.xml",
        |          "rowTag":"phdthesis",
        |          "schema":""
        |        }
        |      },
        |      {
        |        "uuid":"4444",
        |        "name":"ConvertSchema1",
        |        "bundle":"cn.piflow.bundle.common.ConvertSchema",
        |        "properties":{
        |          "schema":"_corrupt_record->corrupt_record,_key->key,_mdate->mdate,_publtype->publtype"
        |        }
        |      },{
        |        "uuid":"5555",
        |        "name":"SelectField1",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |          "schema":"corrupt_record,key,mdate,publtype,author,ee,i,isbn,mouth,note,number,pages,publisher,school,series,sub,sup,title,url,volume,year"
        |        }
        |      },
        |      {
        |        "uuid":"6666",
        |        "name":"PutHiveStreaming1",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |          "database":"dblp",
        |          "table":"dblp_phdthesis"
        |        }
        |      },
        |      {
        |        "uuid":"7777",
        |        "name":"XmlParser2",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |          "xmlpath":"hdfs://10.0.88.70:9000/yqd/dblp/dblp.xml",
        |          "rowTag":"mastersthesis",
        |          "schema":""
        |        }
        |      },
        |      {
        |        "uuid":"8888",
        |        "name":"ConvertSchema2",
        |        "bundle":"cn.piflow.bundle.common.ConvertSchema",
        |        "properties":{
        |          "schema":"_corrupt_record->corrupt_record,_key->key,_mdate->mdate,_publtype->publtype"
        |        }
        |
        |      },{
        |        "uuid":"9999",
        |        "name":"SelectField2",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |          "schema":"corrupt_record,key,mdate,author,school,title,year"
        |        }
        |      },
        |      {
        |        "uuid":"10101010",
        |        "name":"PutHiveStreaming2",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |          "database":"dblp",
        |          "table":"dblp_mastersthesis"
        |        }
        |      },
        |      {
        |        "uuid":"11111111",
        |        "name":"XmlParser3",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |          "xmlpath":"hdfs://10.0.88.70:9000/yqd/dblp/dblp.xml",
        |          "rowTag":"www",
        |          "schema":""
        |        }
        |      },
        |      {
        |        "uuid":"12121212",
        |        "name":"ConvertSchema3",
        |        "bundle":"cn.piflow.bundle.common.ConvertSchema",
        |        "properties":{
        |          "schema":"_corrupt_record->corrupt_record,_key->key,_mdate->mdate,_publtype->publtype"
        |        }
        |
        |      },{
        |        "uuid":"13131313",
        |        "name":"SelectField3",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |          "schema":"corrupt_record,key,mdate,publtype,author,booktitle,cite,crossref,editor,note,title,url,year"
        |        }
        |      },
        |      {
        |        "uuid":"14141414",
        |        "name":"PutHiveStreaming3",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |          "database":"dblp",
        |          "table":"dblp_www"
        |        }
        |      },
        |      {
        |        "uuid":"15151515",
        |        "name":"XmlParser4",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |          "xmlpath":"hdfs://10.0.88.70:9000/yqd/dblp/dblp.xml",
        |          "rowTag":"incollection",
        |          "schema":""
        |
        |        }
        |      },
        |
        |      {
        |        "uuid":"16161616",
        |        "name":"ConvertSchema4",
        |        "bundle":"cn.piflow.bundle.common.ConvertSchema",
        |        "properties":{
        |          "schema":"_corrupt_record->corrupt_record,_key->key,_mdate->mdate,_publtype->publtype"
        |        }
        |
        |      },{
        |        "uuid":"17171717",
        |        "name":"SelectField4",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |          "schema":"corrupt_record,key,mdate,publtype,author,booktitle,crossref,ee,i,pages,sub,sup,title,url,year"
        |        }
        |      },
        |      {
        |        "uuid":"18181818",
        |        "name":"PutHiveStreaming4",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |          "database":"dblp",
        |          "table":"dblp_incollection"
        |        }
        |      },
        |      {
        |        "uuid":"19191919",
        |        "name":"XmlParser5",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |          "xmlpath":"hdfs://10.0.88.70:9000/yqd/dblp/dblp.xml",
        |          "rowTag":"book",
        |          "schema":""
        |        }
        |      },
        |      {
        |        "uuid":"20202020",
        |        "name":"ConvertSchema5",
        |        "bundle":"cn.piflow.bundle.common.ConvertSchema",
        |        "properties":{
        |          "schema":"_corrupt_record->corrupt_record,_key->key,_mdate->mdate,_publtype->publtype"
        |        }
        |      },{
        |        "uuid":"21212121",
        |        "name":"SelectField5",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |          "schema":"corrupt_record,key,mdate,publtype,author,booktitle,editor,ee,isbn,note,pages,publisher,school,series,sub,sup,title,url,volume,year"
        |        }
        |      },
        |      {
        |        "uuid":"22222222",
        |        "name":"PutHiveStreaming5",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |          "database":"dblp",
        |          "table":"dblp_book"
        |        }
        |      },
        |      {
        |        "uuid":"23232323",
        |        "name":"XmlParser6",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |          "xmlpath":"hdfs://10.0.88.70:9000/yqd/dblp/dblp.xml",
        |          "rowTag":"proceedings",
        |          "schema":""
        |        }
        |      },
        |      {
        |        "uuid":"24242424",
        |        "name":"ConvertSchema6",
        |        "bundle":"cn.piflow.bundle.common.ConvertSchema",
        |        "properties":{
        |          "schema":"_corrupt_record->corrupt_record,_key->key,_mdate->mdate,_publtype->publtype"
        |        }
        |      },{
        |        "uuid":"25252525",
        |        "name":"SelectField6",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |          "schema":"corrupt_record,key,mdate,author,booktitle,cite,crossref,editor,ee,i,isbn,journal,note,number,pages,publisher,series,sup,title,url,volume,year"
        |        }
        |      },
        |      {
        |        "uuid":"26262626",
        |        "name":"PutHiveStreaming6",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |          "database":"dblp",
        |          "table":"dblp_proceedings"
        |        }
        |      },
        |      {
        |        "uuid":"27272727",
        |        "name":"XmlParser7",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |          "xmlpath":"hdfs://10.0.88.70:9000/yqd/dblp/dblp.xml",
        |          "rowTag":"inproceedings",
        |          "schema":""
        |        }
        |      },
        |      {
        |        "uuid":"28282828",
        |        "name":"ConvertSchema7",
        |        "bundle":"cn.piflow.bundle.common.ConvertSchema",
        |        "properties":{
        |          "schema":"_corrupt_record->corrupt_record,_key->key,_mdate->mdate,_publtype->publtype"
        |        }
        |      },{
        |        "uuid":"29292929",
        |        "name":"SelectField7",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |          "schema":"corrupt_record,key,mdate,publtype,author,booktitle,cdrom,cite,crossref,editor,ee,i,note,number,pages,sub,sup,title,tt,url,year"
        |        }
        |      },
        |      {
        |        "uuid":"30303030",
        |        "name":"PutHiveStreaming7",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |          "database":"dblp",
        |          "table":"dblp_inproceedings"
        |        }
        |      },
        |      {
        |        "uuid":"31313131",
        |        "name":"XmlParser8",
        |        "bundle":"cn.piflow.bundle.xml.XmlParser",
        |        "properties":{
        |          "xmlpath":"hdfs://10.0.88.70:9000/yqd/dblp/dblp.xml",
        |          "rowTag":"article",
        |          "schema":""
        |        }
        |      },
        |      {
        |        "uuid":"32323232",
        |        "name":"ConvertSchema8",
        |        "bundle":"cn.piflow.bundle.common.ConvertSchema",
        |        "properties":{
        |          "schema":"_corrupt_record->corrupt_record,_key->key,_mdate->mdate,_publtype->publtype"
        |        }
        |      },{
        |        "uuid":"33333333",
        |        "name":"SelectField8",
        |        "bundle":"cn.piflow.bundle.common.SelectField",
        |        "properties":{
        |          "schema":"corrupt_record,key,mdate,publtype,author,booktitle,cdrom,cite,crossref,editor,ee,i,journal,month,note,number,pages,publisher,sub,sup,title,tt,url,volume,year"
        |        }
        |      },
        |      {
        |        "uuid":"26262626",
        |        "name":"PutHiveStreaming8",
        |        "bundle":"cn.piflow.bundle.hive.PutHiveStreaming",
        |        "properties":{
        |          "database":"dblp",
        |          "table":"dblp_article"
        |        }
        |      }
        |    ],
        |    "paths":[
        |      {
        |        "from":"UnzipFilesOnHDFS",
        |        "outport":"",
        |        "inport":"",
        |        "to":"Fork"
        |      },{
        |        "from":"Fork",
        |        "outport":"out1",
        |        "inport":"",
        |        "to":"XmlParser1"
        |      },{
        |        "from":"XmlParser1",
        |        "outport":"",
        |        "inport":"",
        |        "to":"ConvertSchema1"
        |      },{
        |        "from":"ConvertSchema1",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField1"
        |      },{
        |        "from":"SelectField1",
        |        "outport":"",
        |        "inport":"",
        |        "to":"PutHiveStreaming1"
        |      },{
        |        "from":"Fork",
        |        "outport":"out2",
        |        "inport":"",
        |        "to":"XmlParser2"
        |      },{
        |        "from":"XmlParser2",
        |        "outport":"",
        |        "inport":"",
        |        "to":"ConvertSchema2"
        |      },{
        |        "from":"ConvertSchema2",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField2"
        |      },{
        |        "from":"SelectField2",
        |        "outport":"",
        |        "inport":"",
        |        "to":"PutHiveStreaming2"
        |      },{
        |        "from":"Fork",
        |        "outport":"out3",
        |        "inport":"",
        |        "to":"XmlParser3"
        |      },{
        |        "from":"XmlParser3",
        |        "outport":"",
        |        "inport":"",
        |        "to":"ConvertSchema3"
        |      },{
        |        "from":"ConvertSchema3",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField3"
        |      },{
        |        "from":"SelectField3",
        |        "outport":"",
        |        "inport":"",
        |        "to":"PutHiveStreaming3"
        |      },{
        |        "from":"Fork",
        |        "outport":"out4",
        |        "inport":"",
        |        "to":"XmlParser4"
        |      },{
        |        "from":"XmlParser4",
        |        "outport":"",
        |        "inport":"",
        |        "to":"ConvertSchema4"
        |      },{
        |        "from":"ConvertSchema4",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField4"
        |      },{
        |        "from":"SelectField4",
        |        "outport":"",
        |        "inport":"",
        |        "to":"PutHiveStreaming4"
        |      },{
        |        "from":"Fork",
        |        "outport":"out5",
        |        "inport":"",
        |        "to":"XmlParser5"
        |      },{
        |        "from":"XmlParser5",
        |        "outport":"",
        |        "inport":"",
        |        "to":"ConvertSchema5"
        |      },{
        |        "from":"ConvertSchema5",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField5"
        |      },{
        |        "from":"SelectField5",
        |        "outport":"",
        |        "inport":"",
        |        "to":"PutHiveStreaming5"
        |      },{
        |        "from":"Fork",
        |        "outport":"out6",
        |        "inport":"",
        |        "to":"XmlParser6"
        |      },{
        |        "from":"XmlParser6",
        |        "outport":"",
        |        "inport":"",
        |        "to":"ConvertSchema6"
        |      },{
        |        "from":"ConvertSchema6",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField6"
        |      },{
        |        "from":"SelectField6",
        |        "outport":"",
        |        "inport":"",
        |        "to":"PutHiveStreaming6"
        |      },{
        |        "from":"Fork",
        |        "outport":"out7",
        |        "inport":"",
        |        "to":"XmlParser7"
        |      },{
        |        "from":"XmlParser7",
        |        "outport":"",
        |        "inport":"",
        |        "to":"ConvertSchema7"
        |      },{
        |        "from":"ConvertSchema7",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField7"
        |      },{
        |        "from":"SelectField7",
        |        "outport":"",
        |        "inport":"",
        |        "to":"PutHiveStreaming7"
        |      },{
        |        "from":"Fork",
        |        "outport":"out8",
        |        "inport":"",
        |        "to":"XmlParser8"
        |      },{
        |        "from":"XmlParser8",
        |        "outport":"",
        |        "inport":"",
        |        "to":"ConvertSchema8"
        |      },{
        |        "from":"ConvertSchema8",
        |        "outport":"",
        |        "inport":"",
        |        "to":"SelectField8"
        |      },{
        |        "from":"SelectField8",
        |        "outport":"",
        |        "inport":"",
        |        "to":"PutHiveStreaming8"
        |      }
        |    ]
        |  }
        |}
        |
      """.stripMargin*/
    val url = "http://10.0.86.98:8001/flow/start"
    val timeout = 1800
    val requestConfig = RequestConfig.custom()
      .setConnectTimeout(timeout*1000)
      .setConnectionRequestTimeout(timeout*1000)
      .setSocketTimeout(timeout*1000).build()

    val client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build()

    val post:HttpPost = new HttpPost(url)
    post.addHeader("Content-Type", "application/json")
    post.setEntity(new StringEntity(json))


    val response:CloseableHttpResponse = client.execute(post)
    val entity = response.getEntity
    val str = EntityUtils.toString(entity,"UTF-8")
    println("Code is " + str)
  }

}
